-- region Coroutine.lua
-- Author : Bingle
-- Date   : 2015/3/11
-- Description ：协程管理器

Coroutine = class("Coroutine",function()
        local obj = {

            m_coroutine_id = nil,
            onCoroutinCallback = nil,

        }
        return obj
    end)
Coroutine.__index = Coroutine

-- 单例设计
Coroutine.__instance = nil

-- 创建场景管理器
function Coroutine:create()

    local obj = Coroutine:new()

    obj:init()
    Coroutine.__instance = obj

    return obj
end

-- 获取单例
function Coroutine:ins()
    if nil == Coroutine.__instance then
        Coroutine:create()
    end
    return Coroutine.__instance
end

-- 初始化函数
function Coroutine:init()
    local onFrame = function()
        self:onFrame()
    end
    self.scheduler_id = _G._scheduler:scheduleScriptFunc(onFrame,1/60,false)
end

function Coroutine:onFrame()
    local function onCoroutinCallback(dicParam)
        if self.onCoroutinCallback ~= nil then
            self.onCoroutinCallback(dicParam)
        end
    end
    if self.m_coroutine_id ~= nil then
        --log.debug("self.m_coroutine_id",serialized({self.m_coroutine_id},2))
        --log.debug("onCoroutinCallback",serialized({onCoroutinCallback},2))
        coroutine.resume(self.m_coroutine_id,onCoroutinCallback)
    end
end

function Coroutine:run(func,callback)
    self.m_coroutine_id = coroutine.create(func)
    self.onCoroutinCallback = callback
    coroutine.resume(self.m_coroutine_id,callback)
end

function Coroutine:done()
    self.m_coroutine_id = nil 
end

function Coroutine:yield()
    if self.m_coroutine_id ~= nil then
        coroutine.yield()
        -- log.warning("Coroutine:yield","...")
    end
end